import extend from 'extend'
import { modal } from 'vue-strap'
import { showMsg, updateMoney } from '../../../vuex/action'

export default {
  name: 'Import-money',
  components: {
    'vue-modal': modal
  },
  props: ['ids', 'remark', 'title'],
  data() {
    return {
      showImportModal: false,
      update: {
        customerIds: '',
        event: 8,
        gold: '',
        remark: ''
      }
    }
  },
  vuex: {
    actions: {
      xShowMsg: showMsg,
      xUpdateMoney: updateMoney
    }
  },
  ready() {
    if (this.ids) {
      if (typeof this.ids === 'string' || typeof this.ids === 'number') {
        this.update.customerIds = this.ids + ''
      } else {
        this.update.customerIds = this.ids.join(', ')
      }
    }
    if (this.remark) {
      this.update.remark = this.remark + ''
    }
  },
  methods: {
    submit() {
      var update = extend(true, {}, this.update)
      var customerIds = update.customerIds.split(/[ ]*(?:,|，)[ ]*/)
      if (!update.gold) {
        return this.xShowMsg({
          type: 'danger',
          content: '请输入贝塔币数量'
        })
      }
      if (!update.remark) {
        return this.xShowMsg({
          type: 'danger',
          content: '请输入贝塔备注说明'
        })
      }
      this.xUpdateMoney({
        customerIds: JSON.stringify(customerIds),
        event: +update.event,
        gold: +update.gold,
        remark: update.remark
      })
        .then(() => {
          this.showImportModal = false
        })
    }
  }
}
